import {
  Button, Form, Input
} from 'antd';
import { useRequest } from 'ahooks';

import * as APIS from '../../../../constants/api-constants';

interface UpdateQuestionnaireProps {
  questionnaireUuid: string,
  handleAlter: () => void
}

const QuestionnaireInfoUpdate = ({
  questionnaireUuid,
  handleAlter
}: UpdateQuestionnaireProps) => {
  const [form] = Form.useForm();

  /**
   * 修改问卷
   */
  const { run, loading } = useRequest((data) => {
    const { name } = data;

    return {
      url: APIS.QUESTIONNAIRE,
      method: 'PUT',
      data: {
        uuid: questionnaireUuid,
        name
      }
    };
  }, {
    manual: true,
    onSuccess: () => {
      handleAlter();
    },
    onError: () => {
    }
  });

  return (
    <Form
      form={form}
      onFinish={(data) => {
        run(data);
      }}
    >
      <Form.Item
        name="name"
        label="名称"
        rules={[{ required: true, message: '请输入二级菜单名称' }]}
      >
        <Input />
      </Form.Item>
      <Form.Item>
        <Button htmlType="submit" type="primary" loading={loading}>
          保存
        </Button>
      </Form.Item>
    </Form>
  );
};
export default QuestionnaireInfoUpdate;
